﻿using System;
using System.Collections.Generic;
using System.Text;

using Kenly.ChineseSegment.Core;
using Kenly.ChineseSegment.DictionaryLoader;

namespace Kenly.ChineseSegment
{
    /// <summary>
    /// 中文分词算法。
    /// </summary>
    public abstract class WordSegmentBase : IWordSegment
    {
        #region IWordSegment 成员

        private string _separator;
        private List<string> _numberSegmentList;
        private List<string> _nameSegmentList;

        /// <summary>
        /// 分词结果字符串分隔符。
        /// </summary>
        public string Separator
        {
            get
            {
                if (string.IsNullOrEmpty(_separator))
                {
                    _separator = "/";
                }
                return _separator;
            }
            set
            {
                _separator = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public abstract ISegmentDictionary SegmentDictionary
        {
            get;
        }

     
        /// <summary>
        /// 数字分词链表
        /// </summary>
        protected List<string> NumberSegmentList
        {
            get
            {
                return _numberSegmentList;
            }
        }


        /// <summary>
        /// 姓名分词链表
        /// </summary>
        protected List<string> NameSegmentList
        {
            get
            {
                return _nameSegmentList;
            }
        }

        /// <summary>
        /// 分词。
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public string Segment(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return "";
            }

            string[] sentences = TextProcess.SplitSentences(text);
            StringBuilder result = new StringBuilder();

            foreach (string sentence in sentences)
            {
                SegmentSentence(sentence.Trim(), ref result);
            }

            return result.ToString();
        }


        /// <summary>
        /// 对单个句子分词。
        /// </summary>
        /// <param name="sentence"></param>
        /// <param name="result"></param>
        protected abstract void SegmentSentence(string sentence, ref StringBuilder result);

        #endregion

        #region IWordSegment 成员


        public bool LoadDictionary(IDictionaryLoader dictionaryLoader, string dictionaryPath)
        {
            if (dictionaryLoader == null)
            {
                return false;
            }

            return this.SegmentDictionary.Parse(dictionaryLoader.Load(dictionaryPath));
        }

        public bool AppendDictionary(IDictionaryLoader dictionaryLoader, string dictionaryPath)
        {
            if (dictionaryLoader == null)
            {
                return false;
            }

            //将分词链表存储到字典中
            return this.SegmentDictionary.Append(dictionaryLoader.Load(dictionaryPath));
        }

        public bool LoadNameDictionary(IDictionaryLoader dictionaryLoader, string dictionaryPath)
        {
            if (dictionaryLoader == null)
            {
                return false;
            }

            this._nameSegmentList = dictionaryLoader.Load(dictionaryPath);
            return this._nameSegmentList != null && this._nameSegmentList.Count > 0;
        }

        public bool LoadNumberDictionary(IDictionaryLoader dictionaryLoader, string dictionaryPath)
        {
            if (dictionaryLoader == null)
            {
                return false;
            }

            this._numberSegmentList = dictionaryLoader.Load(dictionaryPath);
            return this._numberSegmentList != null && this._numberSegmentList.Count > 0;
        }

        #endregion
    }
}
